from typing import Type
from pydantic import BaseModel, Field

def generate_custom_schema(model: Type[BaseModel]):
    # 使用 `model_json_schema` 替代过时的 `schema` 方法来获取模型的 JSON Schema
    schema = model.model_json_schema()
    custom_schema = {
        "type": "json_schema",
        "json_schema": {
            "name": model.__name__.lower(),
            "schema": {
                "type": "object",
                "properties": {},
                "required": schema.get("required", []),
                "additionalProperties": False
            },
            "strict": True
        }
    }

    # 遍历属性，为每个属性添加描述或其他自定义设置
    for property_name, property_schema in schema.get("properties", {}).items():
        property_details = {
            "type": property_schema["type"],
            "description": property_schema.get("title", "")
        }

        if property_schema["type"] == "array":
            # 处理数组项的 schema
            items = property_schema.get("items", {})
            if "$ref" in items:
                ref_name = items["$ref"].split("/")[-1]
                items_schema = schema.get("$defs", {}).get(ref_name, {})
                property_details["items"] = {
                    "type": "object",
                    "properties": items_schema.get("properties", {}),
                    "required": items_schema.get("required", []),
                    "additionalProperties": False
                }
            else:
                property_details["items"] = items

        custom_schema["json_schema"]["schema"]["properties"][property_name] = property_details

    return custom_schema
